<template>
  <div class="login-panel">
    <h1 class="title">后台管理系统</h1>
    <el-tabs
      type="border-card"
      :stretch="true"
      v-model="currentTab"
      @tab-change="tabChange(currentTab)"
    >
      <el-tab-pane name="account">
        <template #label>
          <span class="icon">
            <el-icon><UserFilled /></el-icon>
            <span class="text">帐号登录</span>
          </span>
        </template>
        <panel-account ref="accountRef" />
      </el-tab-pane>
      <el-tab-pane name="phone">
        <template #label>
          <span class="icon">
            <el-icon><Iphone /></el-icon>
            <span class="text">手机登录</span>
          </span>
        </template>
        <panel-phone ref="phoneRef" />
      </el-tab-pane>
    </el-tabs>
    <div class="control-account">
      <el-checkbox v-model="checkedRemember" label="记住密码" @change="checkboxChange" />
      <el-link type="primary">记住密码</el-link>
    </div>
    <el-button type="primary" class="login-btn" @click="loginAction" size="large"
      >立即登录
    </el-button>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import PanelAccount from '@/views/login/children/panel-account.vue';
import PanelPhone from '@/views/login/children/panel-phone.vue';
import { storage } from '@/utils/storage-util';
import { TOKEN_REMEMBER, TOKEN_TAB } from '@/global/constants';
import { Iphone, UserFilled } from '@element-plus/icons-vue';

const currentTab = ref(storage.get(TOKEN_TAB) ?? 'account');
const checkedRemember = ref(getCheckRememberStatus());
const accountRef = ref<InstanceType<typeof PanelAccount>>();
const phoneRef = ref<InstanceType<typeof PanelPhone>>();

function loginAction(): void {
  const currentForm = currentTab.value;
  if (currentForm === 'account') {
    accountRef.value?.loginByAccount(checkedRemember.value);
  } else {
    phoneRef.value?.loginByPhone();
  }
}

function tabChange(tabName: string | number) {
  storage.set(TOKEN_TAB, tabName);
}

function checkboxChange() {
  storage.set(TOKEN_REMEMBER, checkedRemember.value);
}

function getCheckRememberStatus(): boolean {
  const status = storage.get(TOKEN_REMEMBER);
  return status === 'true' ? true : false;
}
</script>

<style scoped lang="scss">
@import '@/assets/scss/mixin';

.login-panel {
  width: 448px;
  transform: translate(50%, -30%);
  background-color: #fff;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.1);

  .title {
    text-align: center;
    margin-bottom: 15px;
  }

  .icon {
    @include flex(center, center);

    .text {
      margin-left: 5px;
    }
  }

  .control-account {
    margin-top: 12px;
    display: flex;
    justify-content: space-between;
  }

  .login-btn {
    margin-top: 10px;
    width: 100%;
  }
}
</style>
